home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / networking / amitcp / httpd.lha / httpd / http_get.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-18  |  3.2 KB  |  130 lines

  1. /*
  2.  * http_get.c: Handles things associated with GET
  3.  * 
  4.  * Rob McCool
  5.  * 
  6.  */
  7.  
  8.  
  9.  
  10. #include "httpd.h"
  11.  
  12.  
  13. int header_only;
  14. int num_includes;
  15. int allow;
  16. char allow_options;
  17.  
  18.  
  19. void send_file(char *file, FILE *fd, char *args) {
  20.     FILE *f;
  21.     struct stat finfo;
  22.  
  23.     if(!(f=fopen(file,"r"))) {
  24.         log_reason("file permissions deny server access",file);
  25.         unmunge_name(file);
  26.         die(FORBIDDEN,file,fd); /* we've already established that it exists */
  27.     }
  28.     if(!assbackwards) {
  29.         set_content_type(file);
  30.         fstat(fileno(f),&finfo);
  31.         set_content_length(finfo.st_size);
  32.         set_last_modified(finfo.st_mtime);
  33.         send_mime_headers(fd);
  34.     }
  35.     num_includes=0;
  36.     if(!header_only) 
  37.         send_fd(f,fd,args);
  38. }
  39.  
  40.  
  41. void send_node(char *file, char *args, FILE *fd)
  42. {
  43.     struct stat finfo;
  44.  
  45.     if(stat(file,&finfo) == -1) {
  46.         if(errno==ENOENT) {
  47.             log_reason("file does not exist",file);
  48.             unmunge_name(file);
  49.             die(NOT_FOUND,file,fd);
  50.         }
  51.         else {
  52.             log_reason("file permissions deny server access",file);
  53.             unmunge_name(file);
  54.             die(FORBIDDEN,file,fd);
  55.         }
  56.     }
  57.  
  58.     evaluate_access(file,&finfo,M_GET,&allow,&allow_options);
  59.  
  60.     if(!allow) {
  61.         log_reason("client denied by server configuration",file);
  62.         unmunge_name(file);
  63.         die(FORBIDDEN,file,fd);
  64.     }
  65.  
  66.     if(S_ISDIR(finfo.st_mode)) {
  67.         char ifile[MAX_STRING_LEN];
  68.  
  69.         make_full_path(file,index_name,ifile);
  70.         if(stat(ifile,&finfo) == -1) {
  71.             if(allow_options & OPT_INDEXES)
  72.                 index_directory(file,fd);
  73.             else {
  74.                 log_reason("file permissions deny server access",file);
  75.                 unmunge_name(file);
  76.                 die(FORBIDDEN,file,fd);
  77.             }
  78.         }
  79.         else {
  80.             if(file[strlen(file) - 1] != '/') {
  81.                 char url[MAX_STRING_LEN];
  82.                 strcpy_dir(ifile,file);
  83.                 unmunge_name(ifile);
  84.                 construct_url(url,ifile);
  85.                 die(REDIRECT,url,fd);
  86.             }
  87.             send_file(ifile,fd,args);
  88.         }
  89.         return;
  90.     }
  91.     if(S_ISREG(finfo.st_mode))
  92.         send_file(file,fd,args);
  93.     else {
  94.         log_reason("improper file type",file);
  95.         unmunge_name(file);
  96.         die(FORBIDDEN,file,fd); /* device driver or pipe, no permission */
  97.     }
  98. }
  99.  
  100. void process_get(int in, FILE *out, char *m, char *url, char *args) {
  101.     int s;
  102.  
  103.     if(assbackwards && header_only) {
  104.         header_only = 0;
  105.         die(BAD_REQUEST,"Invalid HTTP/0.9 method.",out);
  106.     }
  107.     s=translate_name(url,out);
  108.     switch(s) {
  109.       case STD_DOCUMENT:
  110.         send_node(url,args,out);
  111.         return;
  112.       case REDIRECT_URL:
  113.         die(REDIRECT,url,out);
  114.       case SCRIPT_NCSA:
  115. #if ! defined(NO_HHTP_SCRIPTS) && ! defined(NO_NCSA_EXEC)
  116.         exec_get_NCSA(url,args,out);
  117. #else
  118.         die(NOT_IMPLEMENTED, "Not in this version", out );
  119. #endif
  120.         return;
  121.       case SCRIPT_CGI:
  122. #if ! defined(NO_HHTP_SCRIPTS) && ! defined(NOCGI_SCRIPTS)
  123.         exec_cgi_script(m,url,args,in,out);
  124.         return;
  125. #else
  126.         die(NOT_IMPLEMENTED, "Not in this version", out );
  127. #endif
  128.     }
  129. }
  130.